package org.bite.mybatis.mapper;

import org.apache.ibatis.annotations.*;
import org.bite.mybatis.model.UserInfo;

import java.util.List;

@Mapper
public interface UserInfoMapper {
    //R
    @ResultMap("ResultMap")
    @Select("select * from userinfo")
    List<UserInfo> queryUserInfos();

    @Select("select * from userinfo where id = #{id}")
    UserInfo queryUserInfo(Integer id);

    @Select("select * from userinfo where id = #{id} and username = '${name}'")
    List<UserInfo> queryUserInfoID_Name(String name,Integer id);

    @Select("select * from userinfo where id = #{id} and username = #{name} and age = #{age}")
    UserInfo queryUserInfoOrder(String name,Integer id,Integer age);

    //C
    @Insert("insert into userinfo (username,password,age,gender,phone,delete_flag) " +
            "values(#{username},#{password},#{age},#{gender},#{phone},#{deleteFlag})")
    Integer insert( UserInfo userinfo);

    @Options(useGeneratedKeys = true , keyProperty = "id")
    @Insert("insert into userinfo (username,password,age,gender,phone,delete_flag) " +
            "values(#{userinfo.username},#{userinfo.password},#{userinfo.age},#{userinfo.gender},#{userinfo.phone},#{userinfo.deleteFlag})")
    Integer insertObject(@Param("userinfo") UserInfo userinfo);

    //D
    @Delete(("delete from userinfo where id = #{id}"))
    Integer delete(Integer id);

    //U

    @Update(("update userinfo set password = #{password} where id = #{id}" ))
    Integer update(String password , Integer id);


    //R 处理看到字段与属性不一致的情况
      //起别名 as
    @Select("select id ,username,password,age,gender,phone," +
            "delete_flag as deleteFlag,create_time as createTime,update_time as updateTime from userinfo ")
    List<UserInfo> SelectAll();
        //Results{@Result(}
    @Results(id = "ResultMap",value ={
            @Result(column = "delete_flag",property = "deleteFlag"),
            @Result(column = "create_time",property = "createTime"),
            @Result(column = "update_time",property = "updateTime"),
    })
    @Select("select * from userinfo")
    List<UserInfo> queryUserAll();

      //配置
    @Select("select * from userinfo")
    List<UserInfo> queryUserAll2();

    @Select("select * from userinfo order by id ${order}")
    List<UserInfo> queryUserInfoByOrder(String order);
    @Select("select * from userinfo where username like '%${name}%'")
    List<UserInfo> queryUserInfoByLike(String name);
    @Select("select * from userinfo where username like CONCAT('%',#{name},'%')")
    List<UserInfo> queryUserInfoByLike2(String name);

    @Insert("<script>insert into userinfo (username,password, " +
            "                              <if test=\"age!=null\">age,</if> " +
            "                              gender,phone) " +
            "        values (#{username},#{password}, " +
            "        <if test=\"age!=null\">#{age},</if> " +
            "                #{gender},#{phone})</script>")
    Integer insertByCondition(UserInfo userInfo);
}

